<template lang="html">
  <div class="good">
    <div class="menu-wrapper" ref="menu">
      <ul>
        <li class="menu-item" v-for="(good, index) in goods" :class="{'current': currentIndex == index}" :key="index" @click="selectMenu(index)">
          <span class="text border-1px">
            <span v-if="good.type > 0" class="icon" :class="classMap[good.type]"></span>{{good.name}}
          </span>
        </li>
      </ul>
    </div>
    <div class="food-wrapper" ref="foods">
      <ul>
        <li v-for="good in goods" :key="good.index" class="food-list food-list-hook">
          <h1 class="title">{{good.name}}</h1>
          <ul>
            <li @click="selectFood(food)" class="food-item border-1px" v-for="food in good.foods" :key="food.index">
              <div class="icon">
                <img :src="food.icon" width="57" height="57" alt="">
              </div>
              <div class="content">
                <h2 class="name">{{food.name}}</h2>
                <p class="desc">{{food.description}}</p>
                <div class="extra">
                  <span class="count">月售{{food.sellCount}}份</span><span>好评率{{food.rating}}%</span>
                </div>
                <div class="price">
                  <span class="new">￥{{food.price}}</span><span class="old" v-if="food.oldPrice">￥{{food.oldPrice}}</span>
                </div>
                <div class="cartcontrol-wrapper">
                  <cartcontrol :food="food" @cart-add="_drop"></cartcontrol>
                </div>
              </div>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    <shopcart ref="shopcart" :select-foods="selectFoods" :min-price="seller.minPrice" :delivery-price="seller.deliveryPrice"></shopcart>
    <food :foods="selectedFood" ref="food" @cart-add="_drop"></food>
  </div>
</template>

<script type="text/ecmascript0-6">
import BScorll from 'better-scroll';
import cartcontrol from '@/components/cartcontrol/cartcontrol.vue';
import shopcart from '@/components/shopcart/shopcart.vue';
import food from '@/components/food/food.vue';

const ERR_OK = 0;

export default {
  name: 'good',
  props: {
    seller: {
      type: Object
    }
  },
  data() {
    return {
      goods: [],
      listHeight: [],
      scrollY: 0,
      selectedFood: {}
    };
  },
  components: {
    cartcontrol,
    shopcart,
    food
  },
  created() {
    this.classMap = ['decrease', 'discount', 'special', 'invoice', 'guarantee'];
    this.$http.get('/api/goods').then((response) => {
      response = response.body;
      if (response.errno === ERR_OK) {
        this.goods = response.data;
        this.$nextTick(() => {
          this._initScroll();
          this._calculateHeight();
        });
      }
    });
  },
  computed: {
    currentIndex() {
      for (let i = 0; i < this.listHeight.length; i++) {
        let height1 = this.listHeight[i];
        let height2 = this.listHeight[i + 1];
        if (!height2 || (this.scrollY > height1 && this.scrollY < height2)) {
          return i;
        }
      }
      return 0;
    },
    selectFoods() {
      let foods = [];
      this.goods.forEach((good) => {
        good.foods.forEach((food) => {
          if (food.count) {
              foods.push(food);
          }
        });
      });
      return foods;
    }
  },
  methods: {
    _initScroll() {
      this.menuScroll = new BScorll(this.$refs.menu, {
        click: true
      });

      this.foodsScroll = new BScorll(this.$refs.foods, {
        click: true,
        probeType: 3
      });

      this.foodsScroll.on('scroll', (pos) => {
        this.scrollY = Math.abs(Math.round(pos.y));
      });
    },
    _calculateHeight() {
      let foodHeight = this.$refs.foods.getElementsByClassName('food-list-hook');
      let height = 0;
      this.listHeight.push(height);
      for (let i = 0; i < foodHeight.length; i++) {
        let item = foodHeight[i];
        height += item.clientHeight;
        this.listHeight.push(height);
      }
    },
    _drop(target) {
      // 体验优化，异步执行下落动画
      this.$nextTick(() => {
        this.$refs.shopcart.drop(target);
      });
    },
    selectMenu(index) {
      let foodHeight = this.$refs.foods.getElementsByClassName('food-list-hook');
      let el = foodHeight[index];
      this.foodsScroll.scrollToElement(el, 300);
    },
    selectFood(food) {
      this.selectedFood = food;
      this.$refs.food.show();
    }
  }
};
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import '../../common/stylus/mixin.styl';

  .good
    position: fixed
    display: flex
    top: 174px
    left: 0
    bottom: 48px
    width: 100%
    overflow: hidden
    .menu-wrapper
      flex: 0 0 80px
      width: 80px
      overflow: hidden
      background: #f3f5f7
      .menu-item
        display: table
        width: 56px
        height: 54px
        padding: 0 12px
        &.current
          position: relative
          z-index: 10
          margin-top: -1px
          background: #fff
          font-weight: 700
          .text
            border-none()
        .text
          display: table-cell
          vertical-align: middle
          width: 56px
          line-height: 14px
          border-1px(rgba(7, 17, 27, .1))
          font-size: 12px
          &:last-child
            border-none
          .icon
            display: inline-block
            vertical-align: top
            width: 12px
            height: 12px
            margin-right: 2px
            background-size: 12px 12px
            background-repeat: no-repeat
            &.decrease
              icon-img('decrease_3')
            &.discount
              icon-img('discount_3')
            &.guarantee
              icon-img('guarantee_3')
            &.invoice
              icon-img('invoice_3')
            &.special
              icon-img('special_3')
    .food-wrapper
      flex: 1
      .food-list
        .title
          height: 26px
          padding-left: 14px
          line-height: 26px
          border-left: 2px solid #d9dde1
          font-size: 12px
          color: rgb(147, 153, 159)
          background: #f3f5f7
        .food-item
          display: flex
          margin: 18px
          padding-bottom: 18px
          border-1px(rgba(7, 17, 27, .1))
          &:last-child
            border-none
            margin-bottom: 0
          .icon
            flex: 0 0 57px
            width: 57px
            margin-right: 10px
          .content
            position: relative
            flex: 1
            .name
              margin-top: 2px
              line-height: 14px
              font-size: 14px
              color: rgb(7, 17, 27)
            .desc, .extra
              margin-top: 8px
              line-height: 10px
              font-size: 10px
              color: rgb(147, 153, 159)
              .count
                margin-right: 12px
            .price
              line-height: 24px
              .new
                margin-right: 8px
                font-size: 14px
                font-weight: 700
                color: rgb(240, 20, 20)
              .old
                font-size: 10px
                font-weight: 700
                color: rgb(147, 153, 159)
            .cartcontrol-wrapper
              position: absolute
              bottom: 0
              right: 0
</style>
